<template>
  <el-dialog title="收货结算更新" :close-on-click-modal="false" :visible.sync="visible" center fullscreen>
    <el-button class="miui-dialog-close-btn" @click="visible = false">关闭页面</el-button>
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-row>
        <el-col :span="8">
          <el-form-item label="单据编号" prop="billId">
            <el-input v-model="dataForm.billId" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="订单编号">
            <el-input readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="单据日期">
            <el-date-picker v-model="dataForm.createTime" type="datetime" style="width: 100%;" value-format="yyyy-MM-dd HH:mm:ss"
              readonly></el-date-picker>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="所属制造商">
            <el-input v-model="dataForm.supplyName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="入库类型">
            <el-input value="采购入库" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="接收仓位">
            <el-input v-model="dataForm.inStockName" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="入库审核人">
            <el-input v-model="dataForm.auditUserName" readonly></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="运单号">
            <el-input readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-form-item label="备注" prop="memo">
            <el-input type="textarea" :rows="2" placeholder="请输入内容" v-model="dataForm.memo" readonly></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="12">
          <el-form-item label="计算方式">
            <el-radio v-model="calculate" label="1" @change="changeCalculate">自动计算</el-radio>
            <el-radio v-model="calculate" label="2" @change="changeCalculate">手动计算</el-radio>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <el-table :data="dataForm.list" border v-loading="dataListLoading" header-cell-class-name="miui-header-cell-style">

      <el-table-column prop="goodsCode" label="商品代码"></el-table-column>
      <el-table-column prop="goodsName" label="商品名称"></el-table-column>
      <el-table-column prop="goodsSpec" label="型号"> </el-table-column>
      <el-table-column prop="price" label="零售价"></el-table-column>
      <el-table-column prop="goodsQuantity" label="采购数量"></el-table-column>
      <el-table-column prop="notTaxPrice" label="单位成本">
        <template slot-scope="scope">
          <el-input v-model="scope.row.notTaxPrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="totalNotTaxPrice" label="成本合计">
        <template slot-scope="scope">
          <el-input v-model="scope.row.totalNotTaxPrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="taxRate" label="税率">
        <template slot-scope="scope">
          <el-input v-model="scope.row.taxRate" @change="changeTaxRate(scope.row,scope.$index)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="taxPrice" label="含税单价">
        <template slot-scope="scope">
          <el-input v-model="scope.row.taxPrice" @change="changeTaxPrice(scope.row)"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="taxRatePrice" label="税额合计">
        <template slot-scope="scope">
          <el-input v-model="scope.row.taxRatePrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
      <el-table-column prop="totalTaxPrice" label="价税合计">
        <template slot-scope="scope">
          <el-input v-model="scope.row.totalTaxPrice" :disabled="isDisabled"></el-input>
        </template>
      </el-table-column>
    </el-table>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit" :loading="ischeck" v-prevent-re-click>保存</el-button>
      <el-switch v-model="dataForm.receiptStatus" active-text="保存并审核" inactive-value="1" active-value="2" style="margin-left: 15px;"></el-switch>
    </span>
  </el-dialog>
</template>

<script>
  import {
    receiveApplyGetObj,
    receiveApplyAddObj,
    receiveApplyPutObj
  } from '@/api/eims/purchase/receive-apply'
  import {
    getConsignSettleById,
    putConsignSettleObj
  } from '@/api/eims/consign/consignSettlement'

  export default {
    data() {
      return {
        ischeck: false,
        visible: false,
        calculate: '1',
        dataForm: {
          id: 0,
          ctgId: '',
          ctgName: '',
          supplyId: '',
          supplyName: '',
          memo: '',
          receiptStatus: '1',
          list: []
        },
        isDisabled: true,
        ctgOptions: [],
        dataList: [],
        dataListLoading: false,
        dataRule: {
          ctgId: [{
            required: true,
            message: '请选择采购类别'
          }],
          supplyName: [{
            required: true,
            message: '请选择制造商'
          }]
        },
        supplySearchVisible: false,
        goodsListVisible: false,
        selectionList: []
      }
    },
    methods: {
      init(id) {
        this.dataForm.id = id || 0
        this.dataForm.supplyId = ''
        this.visible = true
        this.dataList = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getConsignSettleById(this.dataForm.id).then(response => {
              if(response.data.code == '0'){
                this.dataForm = response.data.data
                this.dataForm.list.forEach(item => {
                  item.totalNotTaxPrice = (item.goodsQuantity * item.notTaxPrice).toFixed(2)
                  item.totalTaxPrice = (item.goodsQuantity * item.taxPrice).toFixed(2)
                })
              }else{
                this.$message.error(response.data.msg)
              }
            })
          }
        })
      },
      //修改计算方式
      changeCalculate() {
        if (this.calculate == '1') {
          this.isDisabled = true
        } else {
          this.isDisabled = false
        }
      },
      changeTaxRate(data, index) {
        data.notTaxPrice = (data.taxPrice / (1 + data.taxRate / 100)).toFixed(6)
        data.totalNotTaxPrice = (data.goodsQuantity * data.notTaxPrice).toFixed(2)
        data.taxRatePrice = (data.taxPrice / (1 + data.taxRate / 100) * (data.taxRate / 100) * data.goodsQuantity).toFixed(2)
        data.totalTaxPrice = (data.goodsQuantity * data.taxPrice).toFixed(2)
      },
      changeTaxPrice(data) {
        data.notTaxPrice = (data.taxPrice / (1 + data.taxRate / 100)).toFixed(6)
        data.totalNotTaxPrice = (data.goodsQuantity * data.notTaxPrice).toFixed(2)
        data.taxRatePrice = (data.taxPrice / (1 + data.taxRate / 100) * (data.taxRate / 100) * data.goodsQuantity).toFixed(2)
        data.totalTaxPrice = (data.goodsQuantity * data.taxPrice).toFixed(2)
      },
      // 表单提交
      dataFormSubmit() {
        this.ischeck = true
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.detailList = this.dataForm.list
            putConsignSettleObj(this.dataForm).then(response => {
              if(response.data.msg == '0'){
                (this.dataForm.receiptStatus == '1') ? this.$message.success('更新成功'): this.$message.success('结算成功')
                this.visible = false
                this.$emit('refreshDataList')
              }else{
                this.$message.error(response.data.msg)
              }
              this.ischeck = false
            }).catch(() => {
              this.ischeck = false
            })
          }else{
            this.ischeck = false
          }
        })
      },
    }
  }
</script>
